package com.gjj.echarts.echartsgjj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gjj.echarts.echartsgjj.mapper.GjjTableMapper;
import com.gjj.echarts.echartsgjj.pojo.GjjTable;
import com.gjj.echarts.echartsgjj.pojo.GjjTableRetentionpayment;
import com.gjj.echarts.echartsgjj.service.OneMonthService;
import com.gjj.echarts.echartsgjj.vo.*;
import com.sun.org.apache.bcel.internal.generic.MONITORENTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OneMonthServiceImpl implements OneMonthService {
    @Autowired
    GjjTableMapper mapper;
    @Autowired
    RetentionPaymentServiceImpl paymentService;
    @Override
    public OneMonthVo selectOneMonth(Integer month) {
        //要保存到具体的月份中
        OneMonthVo vo = new OneMonthVo();
        vo.setMonth(month);
        //每个月份的具体三个部分
        OnlineChannelVo onlineChannelVo = new OnlineChannelVo();
        CabinetSideChannelVo cabinetSideChannelVo = new CabinetSideChannelVo();
        AutoChannelVo autoChannelVo = new AutoChannelVo();
        //从数据库中快速查出所有符合条件的item
        QueryWrapper<GjjTable> wrapper = new QueryWrapper<>();
        wrapper.eq("month",month);
        List<GjjTable> items = mapper.selectList(wrapper);
        //封装这些item
        for (GjjTable item : items) {
            if (item.getType().equals("委托扣款")){
                onlineChannelVo.setRetentionPayment(item.getPrice());
                onlineChannelVo.setRetentionPaymentUnit(item.getUnitQuantity());
                onlineChannelVo.setRetentionPaymentPeople(item.getPeople());
                RetentionpaymentVo retentionpayment = paymentService.getRetentionPaymentForOneMonth(month);
                onlineChannelVo.setRetentionpaymentVo(retentionpayment);
            } else if (item.getType().equals("建行网银")) {
                onlineChannelVo.setOnlineBankingUnit(item.getUnitQuantity());
                onlineChannelVo.setOnlineBankingPeople(item.getPeople());
                onlineChannelVo.setOnlineBanking(item.getPrice());
            } else if (item.getType().equals("银行卡转账")) {
                cabinetSideChannelVo.setBankAccounting(item.getPrice());
                cabinetSideChannelVo.setBankAccountingPeople(item.getPeople());
                cabinetSideChannelVo.setBankAccountingUnit(item.getUnitQuantity());
            }else if (item.getType().equals("支票")){
                cabinetSideChannelVo.setCheck(item.getPrice());
                cabinetSideChannelVo.setCheckPeople(item.getPeople());
                cabinetSideChannelVo.setCheckUnit(item.getUnitQuantity());
            }else if (item.getType().equals("委托收款")){
                autoChannelVo.setConsignmentCollection(item.getPrice());
                autoChannelVo.setConsignmentCollectionPeople(item.getPeople());
                autoChannelVo.setConsignmentCollectionUnit(item.getUnitQuantity());
            }else {
                cabinetSideChannelVo.setCreditVoucher(item.getPrice());
                cabinetSideChannelVo.setCreditVoucherPeople(item.getPeople());
                cabinetSideChannelVo.setCreditVoucherUnit(item.getUnitQuantity());
            }
        }
        //封装到月份的vo之中
        vo.setAutoChannelVo(autoChannelVo);
        vo.setCabinetSideChannelVo(cabinetSideChannelVo);
        vo.setOnlineChannelVo(onlineChannelVo);

        return vo;
    }

    @Override
    public OneMonthVo selectTwelveMonth() {
        //12个月的就是返回总和
        OneMonthVo twelveMonth = selectOneMonth(1);
        OnlineChannelVo onlineChannelVo = twelveMonth.getOnlineChannelVo();
        CabinetSideChannelVo cabinetSideChannelVo = twelveMonth.getCabinetSideChannelVo();
        AutoChannelVo autoChannelVo = twelveMonth.getAutoChannelVo();
        RetentionpaymentVo retentionpaymentVo = onlineChannelVo.getRetentionpaymentVo();
        twelveMonth.setMonth(13);
        for (int i = 2; i <= 12 ; i++) {
            OneMonthVo month = selectOneMonth(i);
            onlineChannelVo.setRetentionPayment(onlineChannelVo.getRetentionPayment().add(month.getOnlineChannelVo().getRetentionPayment()));
            onlineChannelVo.setOnlineBanking(onlineChannelVo.getOnlineBanking().add(month.getOnlineChannelVo().getOnlineBanking()));
            cabinetSideChannelVo.setBankAccounting(cabinetSideChannelVo.getBankAccounting().add(month.getCabinetSideChannelVo().getBankAccounting()));
            cabinetSideChannelVo.setCheck(cabinetSideChannelVo.getCheck().add(month.getCabinetSideChannelVo().getCheck()));
            cabinetSideChannelVo.setCreditVoucher(cabinetSideChannelVo.getCreditVoucher().add(month.getCabinetSideChannelVo().getCreditVoucher()));
            autoChannelVo.setConsignmentCollection(autoChannelVo.getConsignmentCollection().add(month.getAutoChannelVo().getConsignmentCollection()));
            retentionpaymentVo.setClient(retentionpaymentVo.getClient().add(month.getOnlineChannelVo().getRetentionpaymentVo().getClient()));
            retentionpaymentVo.setCompany(retentionpaymentVo.getCompany().add(month.getOnlineChannelVo().getRetentionpaymentVo().getCompany()));
            retentionpaymentVo.setWebpage(retentionpaymentVo.getWebpage().add(month.getOnlineChannelVo().getRetentionpaymentVo().getWebpage()));
        }
        //TODO 把整年的数据也存到数据库中，直接读取整年的数据

        //这里先写死！整年的公司数不是吧每个月的公司数全部加起来
        onlineChannelVo.setRetentionPaymentUnit(376235);
        onlineChannelVo.setOnlineBankingUnit(21367);
        cabinetSideChannelVo.setBankAccountingUnit(28498);
        cabinetSideChannelVo.setCheckUnit(43789);
        autoChannelVo.setConsignmentCollectionUnit(72369);
        cabinetSideChannelVo.setCreditVoucherUnit(159);
        retentionpaymentVo.setClientUnit(174373);
        retentionpaymentVo.setCompanyUnit(642);
        retentionpaymentVo.setWebpageUnit(201220);

        onlineChannelVo.setRetentionPaymentPeople(5942134);
        onlineChannelVo.setOnlineBankingPeople(697427);
        cabinetSideChannelVo.setBankAccountingPeople(1904692);
        cabinetSideChannelVo.setCheckPeople(191389);
        autoChannelVo.setConsignmentCollectionPeople(564132);
        cabinetSideChannelVo.setCreditVoucherPeople(41289);
        retentionpaymentVo.setClientPeople(4408265);
        retentionpaymentVo.setCompanyPeople(32957);
        retentionpaymentVo.setWebpagePeople(1500912);

        onlineChannelVo.setRetentionpaymentVo(retentionpaymentVo);
        twelveMonth.setOnlineChannelVo(onlineChannelVo);
        twelveMonth.setCabinetSideChannelVo(cabinetSideChannelVo);
        twelveMonth.setAutoChannelVo(autoChannelVo);
        return twelveMonth;
    }
}
